package com.itheima.gongjulei;

import javax.management.relation.RoleUnresolved;
import java.util.concurrent.CountDownLatch;

/*
技术闭锁
 */
public class Demo01 {


    public static void main(String[] args) throws InterruptedException {
        //创建一个计数闭锁   8
        CountDownLatch countDownLatch = new CountDownLatch(8);
        YunDongyuan yunDongyuan = new YunDongyuan(countDownLatch);
        CaiPan caiPan = new CaiPan(countDownLatch);
        new Thread(caiPan).start();
        for (int i = 0; i < 10; i++) {
            Thread t = new Thread(yunDongyuan);
            Thread.sleep(10);
            t.setName(i+"好运动员");
            t.start();
        }


    }

}

class YunDongyuan implements Runnable{
private  CountDownLatch countDownLatch;

    public YunDongyuan(CountDownLatch countDownLatch) {
        this.countDownLatch=countDownLatch;
    }

    @Override
    public void run() {
        //告诉裁判自己已经就绪
        //告诉计数闭锁  -1
        System.out.println(Thread.currentThread().getName()+"已经就位");
        countDownLatch.countDown();
    }
}

class CaiPan  implements Runnable{
    private  CountDownLatch countDownLatch;
    public CaiPan(CountDownLatch countDownLatch) {
        this.countDownLatch=countDownLatch;
    }

    @Override
    public void run() {
        System.out.println("裁判已经就位 ,等待运动员");
        //直接打枪
        //只要等到计数闭锁=0直接干活
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("各就位,预备 嘭!!");
    }
}

